import { checkJoinShow } from '@/servers/api/order';
import { history, useIntl, useModel } from '@umijs/max';
import { Button, Space, message } from 'antd';
import { useEffect, useState } from 'react';
import FormattedMessageCustom from '../FormattedMessageCustom/FormattedMessageCustom';
import ImBtn from '../Im/ImBtn';
interface IParams {
    type: number;
    toMobile: string;
    releaseId: string;
    releaseType: string;
}
const ButtonGroup: React.FC<IParams> = ({
    type,
    toMobile,
    releaseId,
    releaseType,
}) => {
    const [showJoin, setshowJoin] = useState(false);
    const { initialState } = useModel('@@initialState');
    const intl = useIntl();

    function checkShow() {
        checkJoinShow({
            release_id: releaseId,
            type: releaseType,
        }).then((res) => {
            if (res.data.show === true) {
                setshowJoin(true);
            } else {
                setshowJoin(false);
            }
        });
    }
    function partIn() {
        if (initialState?.user) {
            if (toMobile === initialState.user.mobile) {
                message.error(intl.formatMessage({ id: '不能参与自己' }));
                return false;
            }
            history.push('/buildOrder', {
                type: releaseType,
                release_id: releaseId,
            });
        }
    }
    useEffect(() => {
        if (releaseType && releaseId) {
            checkShow();
        }
    }, [releaseType, releaseId]);
    return (
        <Space>
            {type === 2 && showJoin && (
                <Button type="primary" onClick={partIn}>
                    <FormattedMessageCustom id="参与竞标"></FormattedMessageCustom>
                </Button>
            )}
            <ImBtn toMobile={toMobile}></ImBtn>
        </Space>
    );
};
export default ButtonGroup;
